﻿package code
{
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	
	public class enemy extends MovieClip
	{
		
		private var barriers:Vector.<barrier>;
		private var target:Point;
		private var points:Vector.<Point>;
		private var lines:Vector.<myLine>;
		private var spawn:spawnLine;
		private var randomID:Number;
		//private var justSpawned:Boolean;
		
		public function enemy()
		{
			stop();
		}
		
		public function initialize():void
		{
			randomID = Math.random();
			barriers = (parent as map).barrierKids();
			target = new Point(parent.getChildByName("target").x, parent.getChildByName("target").y);
			
			var availSpawns:Vector.<spawnLine> = new Vector.<spawnLine>();
			
			for each (var tempSpawn:spawnLine in(parent as map).spawns)
			{
				if ((Math.abs(tempSpawn.spawnPoint.x - target.x) > 200) || (Math.abs(tempSpawn.spawnPoint.y - target.y) > 200))
				{
					availSpawns.push(tempSpawn);
				}
			}
			spawn = availSpawns[Math.floor(Math.random() * availSpawns.length)];
			addEventListener(Event.ENTER_FRAME, update);
		
		}
		
		private function navMesh():void
		{
			var f:myLine = null;
			var next:Point = new Point();
			var greaterX:Boolean;
			var greaterY:Boolean;
			var veryClose:Boolean = false;
			for each(var tempLine:myLine in lines)
			{
				if (f == null)
				{
					f = tempLine;
				}
				else
				{
					if (Math.abs(target.x - tempLine.randoPoint(this).x) < Math.abs(target.x - f.x))
					{
						if ((target.x >= tempLine.point1.x) && (target.x <= tempLine.point2.x))
						{
							greaterX = true;
						}
					}
					if (Math.abs(target.y - tempLine.randoPoint(this).y) < Math.abs(target.y - f.y))
					{
						if ((target.y >= tempLine.point1.y) && (target.y <= tempLine.point2.y))
						{
							greaterY = true;
						}
					}
					if (greaterX && greaterY)
					{
						f = tempLine;
						if (this.x == f.randoPoint.x && this.y == f.randoPoint.y)
						{
							veryClose = true;
						}
						else
						{
							veryClose = false;
						}
					}
				}
				
			}
			
			spa
			
			if (!veryClose)
			{
				var runThis:Boolean = true;
				while (runThis)
				{
					//if(f.adjSpawn
					//for (var i:int = 0; i <= f.adj
					
				}
			}
			
			/*
			   if ( (Math.abs(x - f.x) > 30) && (Math.abs(y - f.y) > 30))
			   {
			   if (false )//point f is player point
			   {
			   Math.atan2(f.y, f.x);
			   }
			   else
			   {
			   Math.atan2(next.y, next.x);
			   }
			   }
			 */
			
			//var nextClosest:Point = new Point(
			for (var i:int = 0; i < points.length; i++)
			{
				
			}
		
		}
		
		private function generateNavPoints():void
		{
			for each (var navLine:myLine in lines)
			{
				navLine.randomPoint(this,this.width / 2, this.height / 2);
			}
		}
		
		private function pollPlayer():void {
			
		}
		
		private function update(e:Event):void
		{
			navMesh();
			
		}
	}

}
